package fuzzylizard.teamdocs.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Understands a person who uses the teamdocs application. A person has the ability
 * to create new <code>File</code> and so a User object contains a list of all the
 * files that a particular User has created.
 *
 */
public class User extends PersistedObject  implements Serializable {

    private String lastname;
    private String firstname;
    private String username;
    private String password;
    private String email;

    /**
     * A collection of file belonging to a particular user
     */
    private Collection<File> files = new ArrayList<File>();

    /**
     * Static method for creating new Users
     * @param username User's username
     * @param password User's password
     * @return New User
     */
    public static User createUser(String username, String password) {
        return new User(username, password);
    }

    private User(String username, String password) {
        setUsername(username);
        setPassword(password);
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getUsername() {
        return username;
    }

    /* if the username changes, then the user is considered to be a new user */
    private void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void addFile(File file) {
        files.add(file);
    }
    
    public Collection<File> files() {
        return files;
    }

    public void removeFile(File file) {
        files.remove(file);
    }

    public boolean containsFile(File file) {
        return files.contains(file);
    }
    
    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();

        buffer.append(String.valueOf(firstname));
        buffer.append(" ");
        buffer.append(String.valueOf(lastname));
        
        return buffer.toString();
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((username == null) ? 0 : username.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        
        return fieldsEqual((User) obj);
    }

    private boolean fieldsEqual(final User other) {
        return passwordsEqual(other) && usernamesEqual(other);
    }

    private boolean usernamesEqual(final User other) {
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }

    private boolean passwordsEqual(final User other) {
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        return true;
    }

}
